package zzs.generalframework.config.core.database.mysql.datasource;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import zzs.generalframework.config.core.database.mysql.dbconfig.MysqlDatabaseConfig;

/**
 * @title
 * @description
 * @project zzs-config
 * @package zzs.generalframework.config.core.database.mysql.datasource
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Configuration
public class MysqlDataSource
{

	@Autowired
	MysqlDatabaseConfig mysqlDatabaseConfig;

	@Bean("springDataSource")
	@Primary
	public DataSource getSpringDataSource()
	{
		DriverManagerDataSource dataSource = new DriverManagerDataSource();

		dataSource.setDriverClassName(mysqlDatabaseConfig.getDriverClassName());
		dataSource.setUrl(mysqlDatabaseConfig.getDbUrl());
		dataSource.setUsername(mysqlDatabaseConfig.getUsername());
		dataSource.setPassword(mysqlDatabaseConfig.getUserPassword());

		return dataSource;
	}

	@Bean("c3p0DataSource")
	public DataSource getC3P0DataSource()
	{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();

		try
		{
			dataSource.setDriverClass(mysqlDatabaseConfig.getDriverClassName());
			dataSource.setJdbcUrl(mysqlDatabaseConfig.getDbUrl());
			dataSource.setUser(mysqlDatabaseConfig.getUsername());
			dataSource.setPassword(mysqlDatabaseConfig.getUserPassword());
		}
		catch (PropertyVetoException e)
		{
			e.printStackTrace();
		}

		return dataSource;
	}

	@Bean("dbcpDataSource")
	public DataSource getDBCPDataSource()
	{
		BasicDataSource dataSource = new BasicDataSource();

		dataSource.setDriverClassName(mysqlDatabaseConfig.getDriverClassName());
		dataSource.setUrl(mysqlDatabaseConfig.getDbUrl());
		dataSource.setUsername(mysqlDatabaseConfig.getUsername());
		dataSource.setPassword(mysqlDatabaseConfig.getUserPassword());

		return dataSource;
	}

}
